<?php

class utils {

	function create_href($pagename = 'index', $get_data=array() ) {
		if(empty($pagename)) {
			$link = ABS_URL;
		} else {
			$link = ABS_URL . utils::convertw3($pagename) . PAGE_EXTENSION;
			if ( !empty($get_data) ) {
				$link .= '?';
				foreach ($get_data as $k => $v) {
					$link .= $k .'='.htmlspecialchars(utf8_encode($v)).'&amp;';
				}
				$link = substr($link, 0, -5);
			}
		}
		return $link;
	}

  function convertw3 ($string) {
    if( function_exists('mb_convert_encoding') ) {
        $return = mb_convert_encoding($string, "UTF-8"); 
    } else {
        $return = utf8_encode($string);
    }
    
    return (htmlspecialchars($return));
  }
  
  function strip($string) {
    $return = ereg_replace("([^[:alnum:]])+", '_', $string);
    return $return;
  }
  
	function gpc_check($arr) {
		if(!is_array($arr) || empty($arr)) {
			return false;
		} else {
			$data=array();
			foreach($arr as $k=>$v) {
				if(get_magic_quotes_gpc() | get_magic_quotes_runtime()) {
					$data[$k]=$v;
				} else {
					if(is_array($v)) {
						$data[$k] = utils::gpc_check($v);
					} else{
						$data[$k] = (is_numeric($v) ? $v : mysql_real_escape_string(trim($v)) );
					}
				}
			}
			return $data;
		}
	}
  
  
// admin functions
	function checkadminauth() {
		if(!isset($_SESSION['admin_user']) || $_SESSION['admin_user']!=1) {
			$_SESSION['redirect']=$_SERVER['REQUEST_URI'];
			header('Location: index.php');
			exit;
		}
	}
  
	function getpages($wrootcat=0) {
		global $conn;
		$cats=array();
		$where="WHERE id_parent={$wrootcat}";
		$res=mysql_query(" SELECT id_category,page_name FROM pages {$where} ORDER by id_category",$conn);
		if($res!==FALSE && mysql_num_rows($res)) {
			while($lin=mysql_fetch_row($res)) {
				$cats[]=$lin;
			}
			mysql_free_result($res);
			unset($lin);
		}
		return $cats;
	}

	function getpage($id_category) {
		global $conn;
		$cats=array();
		$res=mysql_query(" SELECT * FROM pages WHERE id_category={$id_category} OR id_parent={$id_category}",$conn);
		if($res!==FALSE && mysql_num_rows($res)) {
			while($cats[]=mysql_fetch_assoc($res));
			mysql_free_result($res);
			unset($cats[count($cats)-1]);
			unset($lin);
		}
		return $cats;
	}
  
}// end class